﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using RestaurantPOSManager.DatabaseTableAdapters;
using KhanhPhong.POSDeviceController;
using KhanhPhong.Helper;

namespace RestaurantPOSManager
{
    public partial class FrmPayMoney : Form
    {
        public FrmPayMoney()
        {
            InitializeComponent();
        }

        private Printer obj = null;
        string _ContentPrint = "";
        private string _SoHoaDon = "";

        private void txtTienKhachTra_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                cmdIn.Focus();

                float fTotal = float.Parse(txtTienThanhToan.Value.ToString());
                float fPayment = float.Parse(txtTienKhachTra.Text);

                txtTienThua.Value = fPayment - fTotal;
            }
        }

        private void FrmPayMoney_Load(object sender, EventArgs e)
        {
            txtTienThanhToan.Value = this.Tag.ToString().Split('^')[0];
            _ContentPrint = this.Tag.ToString().Split('^')[1];
            _SoHoaDon = this.Tag.ToString().Split('^')[2];

            string strLogicalName = File.ReadAllText("./LogicalName.conf").Trim();

            try
            {
                obj = new Printer(strLogicalName);
            }
            catch (Exception)
            {
                ChangeButtonStatus();
            }
        }

        private void ChangeButtonStatus()
        {
            cmdIn.Enabled = false;
        }

        private void cmdIn_Click(object sender, EventArgs e)
        {
            try
            {
                // Tổng cộng
                string TongCong = "Tổng".PadRight(4) + " " + Format.FormatCurrency(txtTienThanhToan.Value.ToString()).PadLeft(11);

                // Tiền khách trả
                string KhachTra = "Tiền khách trả".PadRight(14) + " " + Format.FormatCurrency(txtTienKhachTra.Value.ToString()).PadLeft(18);

                // Tiền thừa
                string TienThua = "Tiền dư trả khách".PadRight(17) + " " + Format.FormatCurrency(txtTienThua.Value.ToString()).PadLeft(15);

                // Dữ liệu cuối cùng dùng để in
                string FinalContentPrint = _ContentPrint + Printer.WideCharacter + TongCong + Printer.Normal + "\n" + KhachTra + "\n" + TienThua + Printer.DoubleLineCharacter + "\n" + Printer.CenterAlignment + "So: " + _SoHoaDon + "\n\n" + Printer.LineFeedPaperCut;

                obj.Print(FinalContentPrint);
            }
            catch { }

            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            Close(); // Đóng form
        }

        private void FrmPayMoney_FormClosing(object sender, FormClosingEventArgs e)
        {
            obj.Close();
        }
    }
}
